package com.syt.eduservice.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syt.commonutils.JwtUtils;
import com.syt.commonutils.R;
import com.syt.eduservice.entity.EduCourse;
import com.syt.eduservice.entity.chapter.ChapterVo;
import com.syt.eduservice.entity.frontvo.CourseFrontVo;
import com.syt.eduservice.entity.frontvo.CourseWebVo;
import com.syt.eduservice.feignclient.OrdersClient;
import com.syt.eduservice.service.EduChapterService;
import com.syt.eduservice.service.EduCourseService;
import com.syt.entity.CourseWebVO;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(description = "前台课程展示")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrdersClient ordersClient;

    //1.条件查询分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        return R.ok().data(map);
    }
    //2.课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("test");
        //根据课程Id，编写SQL语句查询课程信息
        CourseWebVo courseFrontVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        Boolean buyCourse=false;
        if (!StringUtils.isEmpty(memberIdByJwtToken)){
           buyCourse = ordersClient.isBuyCourse(courseId, memberIdByJwtToken);
        }

        return R.ok().data("courseWebVo",courseFrontVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }

    //更具课程Id查询课程信息
//    @PostMapping("getCourseInfoOrder/{Id}")
//    public CourseWebVO getCourseInfoOrder(@PathVariable String id){
//        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
//        CourseWebVO courseWebVo = new CourseWebVO();
//        BeanUtils.copyProperties(baseCourseInfo,courseWebVo);
//        return courseWebVo;
//    }
    @PostMapping("{id}")
    public CourseWebVO getCourseInfoOrder(@PathVariable String id){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVO courseWebVo = new CourseWebVO();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVo);
        return courseWebVo;
    }
}
